package com.cn.ccp.action.course;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.util.CCPContants;

 
public class FilePathAction extends BaseAction{
	private Logger log = Logger.getLogger(FilePathAction.class);
    private FilePathBean filePathBean;
	private List<FilePathBean> pathList;
	
	private String selectFlag = "1" ;
	
    public FilePathAction(){
    	super();
    	getpagedetail();
    }
    
    public String goAdd(){
    	return UPDATE;
    }
    
	
 
	public void getpagedetail() {
		pathList = getFilePathService().getAllPaths();
	}
	
	public String edit(){
		log.info("---------------------------");
		if(selectFlag.equals("1"))
			filePathBean.setFlag(true);
		else
			filePathBean.setFlag(false);
		boolean flag = getFilePathService().pathIsUsed(filePathBean.getId(), filePathBean.getPath());
		if(flag){
			showMsg("filePath",  getMessages("course_content_page_upload_file_error_2"));
			return "";
		}
		//更改状态
		if(filePathBean.isFlag()){
			getFilePathService().updateFlag();
		}
		
		if(!filePathBean.getPath().endsWith(File.separator))
			filePathBean.setPath(filePathBean.getPath() +File.separator );
		
		if(filePathBean.getId() == 0){
			try {
				CCPContants.copyFile(CCPContants.SYS_PATH+"tempfile/book-default.png", filePathBean.getPath()+"book-default.png");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return save();
		}
		return update();
	}
	public String save(){
		boolean flag = getFilePathService().saveFilePathBean(filePathBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getFilePathService().updateFilePath(filePathBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}

	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null)
			filePathBean =  (FilePathBean)getFilePathService().findFilePathById(Integer.parseInt(id));
		log.info("-----------------"+filePathBean);
		return UPDATE;
	}
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		getFilePathService().deleteFilePath(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}



	public FilePathBean getFilePathBean() {
		if(filePathBean == null)
			filePathBean = new FilePathBean();
		return filePathBean;
	}



	public void setFilePathBean(FilePathBean filePathBean) {
		this.filePathBean = filePathBean;
	}



	public List<FilePathBean> getPathList() {
		return pathList;
	}



	public void setPathList(List<FilePathBean> pathList) {
		this.pathList = pathList;
	}

	public String getSelectFlag() {
		return selectFlag;
	}

	public void setSelectFlag(String selectFlag) {
		this.selectFlag = selectFlag;
	}
}
